/**
 * 消息主页
 */
var msgData = new Array();
var hasnoread = false; //是否有未读
var localStorageManagers = require("script/localStorageManagers.js");
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true,
	beforeback: function(){
		var parentview = plus.webview.currentWebview().opener();
		parentview.evalJS("setNoreadTag1()");
	}
});
//plus初始化
mui.plusReady(plusReady);
//pluyReady
function plusReady() {
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	//全部标记为已读
	mui('.mui-content').on('tap', '#setallreaded', setAllReaded);
	//点击消息细缆
	mui('.mui-content').on('tap', '.mui-table-view-cell', function() {
		var msgid = this.getAttribute('msgid');
		
		mui.openWindow({
			url: 'msgcontent.html',
			id: 'message/msgcontent',
			styles:getStyles(),
			extras: {
				msgid: msgid
			}
		});
		this.className = 'mui-table-view-cell c1';
	});
	//删除下载项
	 if('iOS'==plus.os.name)mui('.mui-content').on('tap', '.mui-btn-red', deleteMessage);
	 if('Android'==plus.os.name)mui('.mui-content').on('longtap', '.mui-slider-handle', deleteMessage);
	//加载列表
	loadMsgList();
};
//加载消息列表
function loadMsgList() {
	if(mobileFrame.IsNetWorkCanUse()){
		document.getElementById('nettip').style.display='none';
	    ajaxLoadMsg();
	}else{
		document.getElementById('nettip').style.display='block';
	}
}
//ajax加载消息列表
function ajaxLoadMsg() {
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/message/getMsgList.do';
	var w=plus.nativeUI.showWaiting();
	mui.ajax(url, {
		data: {
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			w.close();
			if (data == null || data.length == 0) return;
			msgData = data;
			buildMsgDivs(data);
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			w.close();
			console.log(e1);
		}
	});
}
//构造消息列表
function buildMsgDivs(data) {
	var msgsEle = document.getElementById('discusss-div');
	for (var i = 0; i < data.length; i++) {
		var msg = data[i];
		var topic = msg.topic;
		var statusClass = 'c1';
		var msgname = '通知消息';
		if (msg.status == -1) {
			if (msg.messageType == 2) {
				msgname = '公示消息';
				statusClass = 'c3';
			} else {
				statusClass = 'c2';
			}
			hasnoread = true;
		}
		var delstr='';
		if('iOS'==plus.os.name)delstr='<div class="mui-slider-right mui-disabled"><a class="mui-btn mui-btn-red">删除</a></div>';
		var msgstr = '<li class="mui-table-view-cell ' + statusClass + '" msgid="' + msg.messageId + '">';
		msgstr += delstr;
		msgstr += '<div class="mui-slider-handle discuss-div" discussid="13">';
		msgstr += '<div class="title">';
		msgstr += '<span class="circle">●</span>';
		msgstr += '<span class="d-name">' + msgname + '</span>';
		msgstr += '<span class="d-time">' + msg.sendtime + '</span>';
		msgstr += '</div>';
		msgstr += '<div class="content">' + topic + '</div>';
		msgstr += '</div>';
		msgstr += '</li>';
		msgsEle.innerHTML = msgsEle.innerHTML + msgstr;
	}
	//按钮
	if (hasnoread) document.getElementById('setallreaded').className = 'footbtndiv c2';
}
//全部标记为已读
function setAllReaded() {
	if (!hasnoread) return;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/message/setAllMessageRead.do';
	mui.ajax(url, {
		data: {
			userid: userid,
			msgData: JSON.stringify(msgData)
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			document.getElementById('setallreaded').className = 'footbtndiv c1';
			var lis = document.getElementsByTagName("li");
			for (var i = 0; i < lis.length; i++) {
				lis[i].className = 'mui-table-view-cell c1';
			}
			mui.toast("标记已读成功");
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
}
//删除消息
function deleteMessage() {
	var elem = this;
	var li = elem.parentNode;
	if('iOS'==plus.os.name)li=li.parentNode;
	mui.confirm('确认删除该条消息？', '消息', null, function(e) {
		if (e.index == 0) {
			var msgid = li.getAttribute('msgid');
			var userid = localStorageManagers.getUseridSession();
			var url = serverAjax + 'mobile/message/deleteMessage.do';
			mui.ajax(url, {
				data: {
					userid: userid,
					msgid: msgid
				},
				type: 'post',
				dataType: 'json',
				timeout: 10000,
				success: function(data) {
					mui.toast("删除成功");
					li.parentNode.removeChild(li);
				},
				error: function(e1, e2, e3) {
					ajaxErrorTip();
					console.log(e1);
				}
			});
		} else {
			setTimeout(function() {
				mui.swipeoutClose(li);
			}, 0);
		}
	});
};
//获得消息类别名称
function getMsgTypename(type){
	var name='通知消息';
	//0.课程发布消息 1.课程提问消息  2.笔记消息  3.作业消息  4.研讨消息  5.随堂检测消息'
}
